package pl.mobilebackup.client.main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManagerFactory;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQQueueReceiver;
import org.apache.activemq.ActiveMQQueueSender;
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.ActiveMQSslConnectionFactory;
import org.apache.activemq.BlobMessage;
import org.apache.activemq.broker.SslContext;
import org.apache.activemq.command.ActiveMQQueue;


public class Client {

	public static void main(String[] args) {
		//ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
		System.out.println("Connection: Creating SSL factory");
		ActiveMQSslConnectionFactory sslFactory = new ActiveMQSslConnectionFactory();
		sslFactory.setBrokerURL("ssl://plwaw218:61617");
		
		try {
			String trustAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
			String keyAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
			System.out.println("Trust algorithm: " + trustAlgorithm + ", key algorithm: " + keyAlgorithm);
			
			TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(trustAlgorithm);
			KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(keyAlgorithm);
			
			FileInputStream trustStoreFile = new FileInputStream("ssl/client.ts");
			FileInputStream keyStoreFile = new FileInputStream("ssl/client.ks");
			
			KeyStore keystore = KeyStore.getInstance("JKS");
			keystore.load(trustStoreFile, null);
			trustFactory.init(keystore);
			System.out.println("Truststore initiated");
			
			keystore = KeyStore.getInstance("JKS");
			keystore.load(keyStoreFile, "password".toCharArray());
			keyFactory.init(keystore, "password".toCharArray());
			System.out.println("Keystore initiated");
		    
		    sslFactory.setKeyAndTrustManagers(keyFactory.getKeyManagers(), trustFactory.getTrustManagers(), new SecureRandom());

		} catch (NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (KeyStoreException e) {
			e.printStackTrace();
		} catch (CertificateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (UnrecoverableKeyException e) {
			e.printStackTrace();
		}
		try {
			ActiveMQConnection connection = (ActiveMQConnection) sslFactory.createConnection("pcs", "Wincor2008");
			System.out.println("Connection: Starting SSL connection");
			connection.start();
			System.out.println("Connection: SSL connection started");
//			ActiveMQSession session = (ActiveMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//			ActiveMQQueue queue = (ActiveMQQueue) session.createQueue("ATM_REQUEST");
//			ActiveMQQueue queue2 = (ActiveMQQueue) session.createQueue("ATM_RESPONSE");
//			ActiveMQQueueSender sender = (ActiveMQQueueSender) session.createSender(queue);
//			ActiveMQQueueReceiver receiver = (ActiveMQQueueReceiver) session.createReceiver(queue2);
//		
//			System.out.println("Hello");
//			TextMessage message = session.createTextMessage("Hello");
//			sender.send(message);
//			receiver.receive();
			

//			try {
//				System.out.println("Sending file");
//				FileInputStream file = new FileInputStream("files/test.txt");
//				byte[] fileBytes;
//
//				fileBytes = new byte[file.available()];
//				file.read(fileBytes);
//				
//				BytesMessage fileMessage = session.createBytesMessage();
//				fileMessage.writeBytes(fileBytes);
//				fileMessage.setStringProperty("user", "piomin5");
//				fileMessage.setStringProperty("path", "test.txt");
//				
//				System.out.println(new SimpleDateFormat("yyyy MM dd HH.mm.ss.SSS").format(new Date()));
//				sender.send(fileMessage);
//				System.out.println(new SimpleDateFormat("yyyy MM dd HH.mm.ss.SSS").format(new Date()));
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
			connection.stop();
			connection.close();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}
